Garbage Collection


What is Garbage Collection


In [ ]:

GC in Python

Python takes care of GC automatically for you, and as developer you normally do not have to "worry" about it. But, its still good idea to learn about it, so that you can create your program in such a way that it takes benefit of python's GC.

Previously Python used to solely rely on reference count algorithm to allocate and de-allocate memory. Since, Python 1.5.2 it has changed. Now, it has two strategies for memory allocation

  • Reference Counting
  • Garbage Collection

Reference Counting

Reference counting works by counting the times an object is referenced by other objects. When any reference to an object is removed, its corresponding reference count for an object is decremented and when it becomes zero the object is deallocated.

RC is extremely efficient in memory management but it has few issues. Few of the issues are listed below.

  • reference cycles: It is when there is no way to reach an object but its reference count is still greater than zero. The easiest way to create a reference cycle is to create an object which refers to itself as in the example below:

In [2]:
def cyclic_reference():
    if "arry" not in locals():
        print("create new")
        arry = [ ]
    arry.append(arry)
    print(arry)

In [3]:
cyclic_reference()
cyclic_reference()
cyclic_reference()


create new
[[...]]
create new
[[...]]
create new
[[...]]

In [13]:
good = ["good"]
b = []
b.append(good)
good.append(b)
print( b)
del(good)
print(b)


[['good', [...]]]
[['good', [...]]]

The data of good lives even after we deleted it ...

lets take another example, to validate it further

The living proof


In [26]:
a = ["test"]
b = []
b.append(a)
print(id(a))
print(id(b[0]))
print(b)
del(a)
print(id(b[0]))
print(b)


100528392
100528392
[['test']]
100528392
[['test']]

In [27]:
import gc
print(gc.garbage)
del(b)
gc.collect()
print(gc.garbage)


[]
[]

In [ ]:


In [ ]:


In [ ]:


In [28]:
import gc
print ("Garbage collection thresholds: ", gc.get_threshold())


Garbage collection thresholds:  (700, 10, 10)

What the above information means, is that your application can have about 700 objects before any automatic garbage collector will execute.

  • Space overhead (reference count)

In [ ]:

  • Speed overhead (increment/decrement)

In [ ]:

  • Requires atomicity

In [ ]:

  • Not real-time

In [ ]:

Garbage Collection


In [ ]:


In [ ]:


In [ ]:

References